﻿using BCDto.Sim.Admin.TreePermission;
using BCService.Sim.Admin.TreePermission;
using BCTMSRESTful.Configs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BCTMSRESTful.Controllers.Management.Admin.TreePermission
{
    /// <summary>
    /// 页面权限
    /// </summary>
    [ApiExplorerSettings(GroupName = "Admin")]
    [Route("api/v1/sim/[controller]")]
    public class TreePermissionRecordController : ManagementBaseController
    {
        private readonly ITreePermissionRecordService treePermissionRecordService;

        public TreePermissionRecordController(ITreePermissionRecordService treePermissionRecordService)
        {
            this.treePermissionRecordService = treePermissionRecordService;
        }

        /// <summary>
        /// 添加员工树形权限配置记录
        /// </summary>
        /// <param name="treePermissionConfigId">树形权限配置Id</param>
        /// <param name="staffOrganizationMappingRecordId">员工组织映射记录id</param>
        /// <response code="201">添加员工树形权限配置记录</response>
        /// <returns></returns>
        [HttpPost("[action]/{tree_permission_config_id}/{staff_organization_mapping_record_id}")]
        [Log("添加员工树形权限配置记录")]
        [ProducesResponseType(typeof(TreePermissionRecordDto), 200)]
        public IActionResult SettingAdmin([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId,
            [FromRoute(Name = "staff_organization_mapping_record_id")] long staffOrganizationMappingRecordId)
        {
            var result = this.treePermissionRecordService.SettingAdmin(this.Admin, treePermissionConfigId, staffOrganizationMappingRecordId);
            return this.Ok(result);
        }

        /// <summary>
        /// 添加部门树形权限配置记录
        /// </summary>
        /// <param name="treePermissionConfigId">树形权限配置Id</param>
        /// <param name="departmentId">部门id</param>
        /// <response code="201">添加部门树形权限配置记录</response>
        /// <returns></returns>
        [HttpPost("[action]/{tree_permission_config_id}/{department_id}")]
        [Log("添加部门树形权限配置记录")]
        [ProducesResponseType(typeof(TreePermissionRecordDto), 200)]
        public IActionResult SettingDepartment([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId,
            [FromRoute(Name = "department_id")] int departmentId)
        {
            var result = this.treePermissionRecordService.SettingDepartment(this.Admin, treePermissionConfigId, departmentId);
            return this.Ok(result);
        }

        /// <summary>
        /// 添加公司树形权限配置记录
        /// </summary>
        /// <param name="treePermissionConfigId">树形权限配置Id</param>
        /// <param name="companyId">公司id</param>
        /// <response code="201">添加公司树形权限配置记录</response>
        /// <returns></returns>
        [HttpPost("[action]/{tree_permission_config_id}/{company_id}")]
        [Log("添加公司树形权限配置记录")]
        [ProducesResponseType(typeof(TreePermissionRecordDto), 200)]
        public IActionResult SettingCompany([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId,
            [FromRoute(Name = "company_id")] int companyId)
        {
            var result = this.treePermissionRecordService.SettingCompany(this.Admin, treePermissionConfigId, companyId);
            return this.Ok(result);
        }

        /// <summary>
        /// 删除员工树形权限配置记录
        /// </summary>
        /// <param name="treePermissionConfigId">树形权限配置Id</param>
        /// <param name="staffOrganizationMappingRecordId">员工组织映射记录id</param>
        /// <response code="204">删除员工树形权限配置记录</response>
        /// <returns></returns>
        [HttpDelete("[action]/{tree_permission_config_id}/{staff_organization_mapping_record_id}")]
        [Log("删除员工树形权限配置记录")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult DeleteAdmin([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId,
            [FromRoute(Name = "staff_organization_mapping_record_id")] long staffOrganizationMappingRecordId)
        {
            this.treePermissionRecordService.DeleteAdmin(treePermissionConfigId, staffOrganizationMappingRecordId);
            return this.NoContent();
        }

        /// <summary>
        /// 删除部门树形权限配置记录
        /// </summary>
        /// <param name="treePermissionConfigId">树形权限配置Id</param>
        /// <param name="departmentId">部门id</param>
        /// <response code="204">删除部门树形权限配置记录</response>
        /// <returns></returns>
        [HttpDelete("[action]/{tree_permission_config_id}/{department_id}")]
        [Log("删除部门树形权限配置记录")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult DeleteDepartment([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId,
            [FromRoute(Name = "department_id")] int departmentId)
        {
            this.treePermissionRecordService.DeleteDepartment(treePermissionConfigId, departmentId);
            return this.NoContent();
        }

        /// <summary>
        /// 删除公司树形权限配置记录
        /// </summary>
        /// <param name="treePermissionConfigId">树形权限配置Id</param>
        /// <param name="companyId">公司id</param>
        /// <response code="204">删除公司树形权限配置记录</response>
        /// <returns></returns>
        [HttpDelete("[action]/{tree_permission_config_id}/{company_id}")]
        [Log("删除公司树形权限配置记录")]
        [ProducesResponseType(typeof(NoContentResult), 204)]
        public IActionResult DeleteCompany([FromRoute(Name = "tree_permission_config_id")] long treePermissionConfigId,
            [FromRoute(Name = "company_id")] int companyId)
        {
            this.treePermissionRecordService.DeleteCompany(treePermissionConfigId, companyId);
            return this.NoContent();
        }

        /// <summary>
        /// 获取树形权限配置记录
        /// </summary>
        /// <param name="companyId">公司ID</param>
        /// <param name="departmentId">部门ID</param>
        /// <param name="staffOrganizationMappingRecordId">员工组织映射记录ID</param>
        /// <response code="200">获取树形权限配置记录</response>
        /// <returns></returns>
        [HttpGet]
        [ProducesResponseType(typeof(IEnumerable<TreePermissionRecordViewDto>), 200)]
        public IActionResult List([FromQuery(Name = "company_id")]int? companyId,
            [FromQuery(Name = "department_id")]int? departmentId,
            [FromQuery(Name = "staff_organization_mapping_record_id")]long? staffOrganizationMappingRecordId)
        {
            var result = this.treePermissionRecordService.GetPermissionTemplateTree(companyId, departmentId, staffOrganizationMappingRecordId);
            return this.Ok(result);
        }
    }
}
